local shichou = fk.CreateSkill {

  name = "joysp__shichou",

  tags = {  },

}



shichou:addEffect(fk.AfterCardTargetDeclared, {
  name = "joysp__shichou",
  anim_type = "offensive",
  events = {fk.AfterCardTargetDeclared,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if  target == player and player:hasSkill(shichou.name) and data.card.trueName == "slash" then
      if event == fk.AfterCardTargetDeclared  then
        local current_targets = data:getAllTargets()
        for _, p in ipairs(player.room.alive_players) do
          if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
              data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
            return true
          end
        end
      elseif event == fk.CardUseFinished then
        return   player:getMark("joysp__shichou-turn") > 0 or not data.damageDealt 
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local current_targets = data:getAllTargets()
      local targets = {}
      for _, p in ipairs(room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          table.insert(targets, p.id)
        end
      end
      local n = player:getLostHp() + 1
      local tos = room:askForChoosePlayers(player, targets, 1, n,
      "#joysp__shichou-choose:::"..data.card:toLogString()..":"..tostring(n), shichou.name, true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    elseif event == fk.CardUseFinished then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insertTable(data.tos, table.map(event:getCostData(self), function (p)
        return {p}
      end))
    elseif not data.damageDealt and not player.dead then
      local ids = player.room:getCardsFromPileByRule("slash|.|.", 1, "DrawPile")
      if #ids > 0 then
        player.room:obtainCard(player, ids[1], true, fk.ReasonPrey)
      end
    elseif player:getMark("joysp__shichou-turn") > 0 and not player.dead then
      player:drawCards(player:getMark("joysp__shichou-turn"),shichou.name)
      player.room:setPlayerMark(player,"joysp__shicho-turn",0)
    end
  end,
})
shichou:addEffect(fk.CardUseFinished, {
  name = "joysp__shichou",
  anim_type = "offensive",
  events = {fk.AfterCardTargetDeclared,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if  target == player and player:hasSkill(shichou.name) and data.card.trueName == "slash" then
      if event == fk.AfterCardTargetDeclared  then
        local current_targets = data:getAllTargets()
        for _, p in ipairs(player.room.alive_players) do
          if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
              data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
            return true
          end
        end
      elseif event == fk.CardUseFinished then
        return   player:getMark("joysp__shichou-turn") > 0 or not data.damageDealt 
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local current_targets = data:getAllTargets()
      local targets = {}
      for _, p in ipairs(room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          table.insert(targets, p.id)
        end
      end
      local n = player:getLostHp() + 1
      local tos = room:askForChoosePlayers(player, targets, 1, n,
      "#joysp__shichou-choose:::"..data.card:toLogString()..":"..tostring(n), shichou.name, true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    elseif event == fk.CardUseFinished then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insertTable(data.tos, table.map(event:getCostData(self), function (p)
        return {p}
      end))
    elseif not data.damageDealt and not player.dead then
      local ids = player.room:getCardsFromPileByRule("slash|.|.", 1, "DrawPile")
      if #ids > 0 then
        player.room:obtainCard(player, ids[1], true, fk.ReasonPrey)
      end
    elseif player:getMark("joysp__shichou-turn") > 0 and not player.dead then
      player:drawCards(player:getMark("joysp__shichou-turn"),shichou.name)
      player.room:setPlayerMark(player,"joysp__shicho-turn",0)
    end
  end,
})
shichou:addEffect(fk.Damage, {
  refresh_events = {fk.Damage},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(shichou.name) and data.card and data.card.trueName == "slash" and player:getMark("joysp__shichou_max-turn") < 3
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player,"joysp__shichou_max-turn",1)
    room:addPlayerMark(player,"joysp__shichou-turn",1)
  end,
})

return shichou